<?php

namespace App\Service\H5;

use App\Constants\ErrorCode;
use App\Helpers\ImUtil;
use App\Model\User;
use App\Model\UserCoin;
use App\Model\UserDevice;
use App\Model\UserForbidden;
use App\Model\UserFreezeLog;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 封禁账号
 */
class UserFreezeService extends BaseService
{
    public function handle()
    {
        $userId = $this->request->input('uid', 1);;

        $profileId = $this->request->input('freeze_uid', 1);
        log_info('用户ID+++'.$userId."aaaa".$profileId);
        $type = $this->request->input('type', 3);
        $detail = $this->request->input('detail', '');
        $opType = $this->request->input('op_type', 1);
        //是否管理员权限
        if (!User::isSuperAdmin($userId)) {
            return $this->setError(ErrorCode::OP_NOT_VALID, '暂无操作权限');
        }
        //用户不存在
        $profile = UserProfile::getProfile($profileId);
        if (!$profile) {
            return $this->setError(ErrorCode::USER_NOT_VALID, '该用户不存在，或信息错误～');
        }

        $act = UserFreezeLog::ACT_FREEZE;
        $status = User::STATUS_FORBID;
        switch ($opType) {
                //禁言1天
            case 1:
                $act = UserFreezeLog::ACT_FORBID;
//                User::where('id', $profileId)->limit(1)->update(['status' => User::STATUS_FORBID]);
                UserForbidden::doForbidden($profileId, 24 * 60);
                break;
                //禁言3天
            case 2:
                $act = UserFreezeLog::ACT_FORBID;
//                User::where('id', $profileId)->limit(1)->update(['status' => User::STATUS_FORBID]);
                UserForbidden::doForbidden($profileId, 24 * 60 * 3);
                break;
            case 3:
                //冻结设备
//                $status = User::STATUS_FREEZE;
////                UserDevice::freezeUserDevice($profileId);
////                User::where('id', $profileId)->limit(1)->update(['status' => User::STATUS_FREEZE]);
//                ImUtil::kickOut($profileId);
            case 4:
                //冻结账户
                $status = User::STATUS_FREEZE;
//                User::where('id', $profileId)->limit(1)->update(['status' => User::STATUS_FREEZE]);
                //融云踢下线
                ImUtil::kickOut($profileId);
                break;
        }
        //封禁记录
        UserFreezeLog::addRecord($profileId, $type, $act, $detail, $userId);

        User::where('id', $profileId)->limit(1)->update(['status' => $status]);

        return ['result' => 1];
    }
}
